package cn.fivestars.springcloud.payment.controller;

import cn.fivestars.springcloud.commons.entities.CommonResult;
import cn.fivestars.springcloud.commons.entities.Payment;
import cn.fivestars.springcloud.payment.serivce.PaymentService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * PaymentController
 *
 * @author liangchenzhou1024@163.com
 * @date 2021/2/1
 */
@RestController
@RequestMapping("/payment")
@Slf4j
@RefreshScope
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/")
    public CommonResult<Payment> save(@RequestBody Payment payment) {
        int result = paymentService.insert(payment);
        if (result > 0) {
            log.info("serverPort:{}, success, payment={}", serverPort, payment);
            return CommonResult.success(payment);
        } else {
            log.error("serverPort:{}, error, payment={}", serverPort, payment);
            return CommonResult.failure(40001, "error", payment);
        }

    }

    @GetMapping("/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id) {
        Payment payment = paymentService.selectById(id);
        if (payment != null) {
            log.info("serverPort:{}, success, payment={}", serverPort, payment);
            return CommonResult.success(payment);
        } else {
            log.error("serverPort:{}, error, id={}", serverPort, id);
            return CommonResult.failure(40002, "no records");
        }
    }

    @GetMapping("/timeout")
    public CommonResult<String> timeOut() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            log.error("thread interrupted:", e);
        }

        return CommonResult.success("success");
    }

    @GetMapping("/server")
    public CommonResult<String> server() {
        return CommonResult.success(String.format("serverPort:%s", serverPort));
    }

    @GetMapping("/discovery")
    public Object discovery() throws JsonProcessingException {
        List<String> services = discoveryClient.getServices();
        log.info("services:{}", services);
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        log.info("instances={}", new ObjectMapper().writeValueAsString(instances));
        return discoveryClient;
    }

    @Value("${version}")
    private String version;

    @GetMapping("/version")
    public CommonResult<String> version() {
        return CommonResult.success(String.format("version:%s", version));
    }
}
